package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.AdminAdminPermissionBO;
import com.cskaoyan.bean.PO.MarketRole;
import com.cskaoyan.bean.VO.AdminRolePermissionsVO;
import com.cskaoyan.bean.VO.BrandVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminRoleService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName AdminRoleController
 * @Description
 * @Author Lxz
 * @Date 2022-8-12 22:51
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;

    @RequiresPermissions("admin:admin:list")
    @RequestMapping("options")
    public BaseRespVo options() {
        CommonData options = adminRoleService.options();
        return BaseRespVo.ok(0,options,"成功");
    }

    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    public BaseRespVo list(String name, BasePageInfo info) {
        CommonData options = adminRoleService.list(name,info);
        return BaseRespVo.ok(0,options,"成功");
    }

    @RequiresPermissions("admin:role:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketRole marketRole) {
        if (StringUtils.isEmpty(marketRole.getName())) {
            return BaseRespVo.custom(502,null,"角色名称不能为空");
        }

        int result = adminRoleService.judgeRepeat(marketRole);
        if (result != 0) {
            return BaseRespVo.custom(640,null,"角色已经存在");
        }
        MarketRole createResult = adminRoleService.create(marketRole);
        return BaseRespVo.ok(0,createResult,"成功");
    }

    @RequiresPermissions("admin:role:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketRole marketRole) {
        if (StringUtils.isEmpty(marketRole.getName())) {
            return BaseRespVo.custom(502,null,"角色名称不能为空");
        }

        int result = adminRoleService.judgeRepeat(marketRole);
        if (result != 0) {
            return BaseRespVo.custom(640,null,"角色名称已经存在");
        }
        adminRoleService.update(marketRole);
        return BaseRespVo.ok(0,null,"成功");
    }

    @RequiresPermissions("admin:role:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole marketRole) {
        String[] baseRoles = {"超级管理员", "商场管理员", "推广管理员"};
        for (String baseRole : baseRoles) {
            if (baseRole.equals(marketRole.getName())) {
                return BaseRespVo.invalidParameter("该角色不可删除");
            }
        }
        adminRoleService.delete(marketRole);
        return BaseRespVo.ok(0,null,"成功");
    }

    @RequiresPermissions("admin:role:permission:get")
    @GetMapping("permissions")
    public BaseRespVo permissionsGet(Integer roleId) {
        AdminRolePermissionsVO permissions = adminRoleService.getPermissions(roleId);
        return BaseRespVo.ok(0,permissions,"成功");
    }

    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo permissionsPost(@RequestBody AdminAdminPermissionBO permissionBO) {
        if (permissionBO.getPermissions().length == 0) {
            return BaseRespVo.invalidParameter("权限不可为空");
        }
        adminRoleService.postPermisssions(permissionBO);
        return BaseRespVo.ok(0,null,"成功");
    }

}
